/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TestUnits;

import javax.microedition.lcdui.*;

public class Imagen extends Canvas implements CommandListener {

    private MoverImagen midlet;
    private Image im;
    private Command salir;
    private int x, y;
    
public Imagen(MoverImagen mid){
    salir = new Command("Salir",Command.EXIT,1);
    this.midlet = mid;
    this.addCommand(salir);
    this.setCommandListener(this);
    
    try{
        im = Image.createImage("C:/Users/user1/Documents/UVG/dibujo.png");
    }
    catch (Exception e){
        System.out.println("Error al cargar archivo de imagen");
    }
    x = 0;
    y = 0;
}
public void paint(Graphics g) {
    g.setColor(255,255,255);
    g.fillRect(0,0,getWidth(),getHeight());
    g.setColor(0,0,0);
    g.drawRoundRect(x,y,20,20,5,5);
    g.drawImage(im,x,y,Graphics.TOP|Graphics.LEFT);
}
protected void keyPressed(int keyCode) {
    switch(getGameAction(keyCode)){
        case Canvas.DOWN: { if ((y+20)<getHeight()) y = y+1;
                            break; }
        case Canvas.LEFT: { if (x > 0) x = x-1;
                            break;}
        case Canvas.UP: { if (y > 0) y = y-1;
                             break;}
        case Canvas.RIGHT:{ if ((x+20) < getWidth()) x = x+1;
                              break;}
        }
    this.repaint();
}
public void commandAction(Command c, Displayable d){
    if (c == salir){
    midlet.salir();
    }
}
}
